﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Samples.WindowsPhoneCloud.StorageClient;
using System.Text;


namespace AzureTracer.Model
{
    public class LogEntity : TableServiceEntity, IEquatable<LogEntity>
    {
        const int MAXLINES_MESSAGEVIEW = 3;

        public long EventTickCount { get; set; }
        public string DeploymentId { get; set; }
        public string Role { get; set; }
        public string RoleInstance { get; set; }
        public int Level { get; set; }
        public int EventId { get; set; }

        public string CultureTimestamp
        {
            get { return Timestamp.ToString(); }
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set
            {
                _message = value;
                VisibleMessage = ShortenMessage(_message);
                LowerMessage = VisibleMessage.ToLowerInvariant();
            }
        }

        private static string ShortenMessage(string message)
        {
            StringBuilder shortenMessage = new StringBuilder();
            var lines = message.Split('\n');
            int index = 0;
            while (index <= MAXLINES_MESSAGEVIEW && index < lines.Length)
            {
                if (index != 0)
                    shortenMessage.Append("\n");

                if (lines[index].Length > 50)
                    shortenMessage.Append(lines[index].Substring(0, 50));
                else
                    shortenMessage.Append(lines[index]);
                index++;
            }
            return shortenMessage.ToString();
        }

        // Message to display in ListView
        public string VisibleMessage { get; set; }
        // Message to improve find feature
        public string LowerMessage { get; set; }

        private bool _isRead;
        public bool IsRead
        {
            get
            { return _isRead; }
            set
            {
                if (_isRead != value)
                {
                    _isRead = value;
                    OnPropertyChanged("IsRead");
                }
            }
        }

        public bool Equals(LogEntity other)
        {
            return this.PartitionKey == other.PartitionKey && this.RowKey == other.RowKey;
        }
    }
}
